home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / DOS / TELECOMM / PCCP047 / XMCRCS.C < prev    next >
Text File  |  1992-07-10  |  3KB  |  187 lines

  1. /*    Copyright (C) 1992 Peter Edward Cann, all rights reserved.
  2.  *    MicroSoft QuickC
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<bios.h>
  7. #include<dos.h>
  8. #include<fcntl.h>
  9. #include<sys\types.h>
  10. #include<sys\stat.h>
  11. #include<signal.h>
  12. #include"port.h"
  13.  
  14. #define NAK 21
  15. #define ACK 6
  16. #define SOH 1
  17. #define EOT 4
  18. #define CAN 24
  19.  
  20. sendchar(c)
  21.     unsigned char c;
  22.     {
  23.     while(!((inp(basereg+STATREG)&TXMTMASK)&&(inp(basereg+MSTATREG)&CTSMASK)));
  24.     outp(basereg, c);
  25.     }
  26.  
  27. int follow;
  28.  
  29. int rcharto(ticks)
  30.     int ticks;
  31.     {
  32.     long tstamp, tstamp1, dayofticksp;
  33.     int c;
  34.     _bios_timeofday(_TIME_GETCLOCK, &tstamp);
  35.     dayofticksp=0;
  36.     while(1)
  37.         {
  38.         if(kbhit())
  39.             getch();
  40.         if(_bios_timeofday(_TIME_GETCLOCK, &tstamp1))
  41.             dayofticksp+=20*60*60*24;
  42.         if(tstamp1+dayofticksp-tstamp>ticks)
  43.             return(-1); /* NOTE: This is an INT!!! */
  44.         if(follow!=index)
  45.             {
  46.             c=buf[follow++];
  47.             follow=follow%TBUFSIZ;
  48.             return(c);
  49.             }
  50.         }
  51.     }
  52.  
  53. int calccrc(ptr, count)
  54.     char *ptr;
  55.     int count;
  56.     {
  57.     int crc, i;
  58.     crc = 0;
  59.     while(--count >= 0)
  60.         {
  61.         crc = crc ^ (int)*ptr++ << 8;
  62.         for(i = 0; i < 8; ++i)
  63.             if(crc & 0x8000)
  64.                 crc = crc << 1 ^ 0x1021;
  65.             else
  66.                 crc = crc << 1;
  67.         }
  68.     return (crc & 0xFFFF);
  69.     }
  70.  
  71. unsigned char block[128];
  72.  
  73. sblock(blockn)
  74.     int blockn;
  75.     {
  76.     unsigned char c;
  77.     unsigned short crc, rcrc;
  78.     int i;
  79.     crc=calccrc(block, 128);
  80.     sendchar(SOH);
  81.     sendchar(blockn);
  82.     sendchar((blockn^0xff)&0xff);
  83.     for(i=0;i<128;++i)
  84.         sendchar(block[i]);
  85.     sendchar((crc>>8)&0xff);
  86.     sendchar(crc&0xff);
  87.     }
  88.  
  89. quit()
  90.     {
  91.     cleanup(0);
  92.     exit(99);
  93.     }
  94.  
  95. main(argc, argv)
  96.     int argc;
  97.     char **argv;
  98.     {
  99.     int i, j, infd, ok, c;
  100.     unsigned char blocknum;
  101.     long nbytes;
  102.     index=follow=0;
  103.     printf("Copyright (C) 1992 Peter Edward Cann, all rights reserved.\n");
  104.     printf("xmodem crc send of %s.\n", argv[4]);
  105.     if(argc!=5)
  106.         {
  107.         printf("USAGE: xmodemr <comnum> <bps> <stopbits> <file pathname>\n");
  108.         exit(1);
  109.         }
  110.     if((infd=open(argv[4], O_RDONLY|O_BINARY))==-1)
  111.         {
  112.         printf("Error opening file %s.\n", argv[4]);
  113.         exit(2);
  114.         }
  115.     comnum=atoi(argv[1])-1;
  116.     speed=atoi(argv[2]);
  117.     databits='8';
  118.     parity='n';
  119.     stopbits=argv[3][0];
  120.     setport();
  121.     signal(SIGINT, quit);
  122.     readset();
  123.     setup();
  124.     nbytes=0;
  125.     if(rcharto(2000)!='C')
  126.         {
  127.         printf("Spurrious char or no C in 100 seconds.\n");
  128.         cleanup(0);
  129.         exit(10);
  130.         }
  131.     blocknum=1;
  132.     while(1)
  133.         {
  134.         if((j=read(infd, block, 128))==0)
  135.             {
  136.             printf("\nEnd of file.\n");
  137.             sendchar(EOT);
  138.             do
  139.                 c=rcharto(300);
  140.             while((c!=ACK)&&(c!=NAK)&&(c!=CAN)&&(c!=-1));
  141.             if(c!=ACK)
  142.                 {
  143.                 printf("No ACK of EOT.\n");
  144.                 cleanup(0);
  145.                 exit(13);
  146.                 }
  147.             else
  148.                 {
  149.                 printf("Successful.\n");
  150.                 cleanup(0);
  151.                 exit(0);
  152.                 }
  153.             }
  154.         for(c=j;c<128;c++)
  155.             block[c]=26;
  156.         i=0;
  157.         do
  158.             {
  159.             printf("\nSending block %d. ", blocknum);
  160.             sblock(blocknum);
  161.             do
  162.                 c=rcharto(200);
  163.             while((c!=ACK)&&(c!=NAK)&&(c!=CAN)&&(c!=-1));
  164.             }
  165.         while((c==NAK)&&(i++<10));
  166.         if(c!=ACK)
  167.             if(c==NAK)
  168.                 {
  169.                 printf("\nRetry limit exceeded.\n");
  170.                 cleanup(0);
  171.                 exit(14);
  172.                 }
  173.             else
  174.                 {
  175.                 printf("\nSpurrious character hex %02x; ACK or NAK expected.\n", c);
  176.                 cleanup(0);
  177.                 exit(11);
  178.                 }
  179.         nbytes+=128;
  180.         printf("Successful. Bytes so far: %ld", nbytes);
  181.         blocknum++;
  182.         }
  183.     printf("Programming error; fell through end; see code.\n");
  184.     cleanup(0);
  185.     exit(12);
  186.     }
  187.